<template>
  <div>
    <el-dialog
      :title="obj.isAdd == false ? '角色添加' : '角色编辑'"
      :visible.sync="obj.isTrue"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" v-model="form.menus">
          <el-tree
            :data="menuList"
            show-checkbox
            node-key="id"
            ref="tree"
            :default-checked-keys="[0]"
            :props="{
              children: 'children',
              label: 'title',
            }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.msg"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit()" v-if="obj.isAdd==false">添加</el-button>
          <el-button type="primary" @click="dit()" v-else>编辑</el-button>
          <el-button @click="cel()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { roleAdd, roleList, menuList, roleInfo, roleEdit } from "../../../requster/api";
import { successAlert, warningAlert } from "../../../util/alter";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        rolename: "", //角色名称
        menus: "",
        status: 1, //状态
      },
    };
  },
  computed: {
    ...mapGetters({
      roleList: "role/list",
      menuList: "menu/list",
    }),
  },
  methods: {
    ...mapActions({
      reqroleList: "role/reqchangeList",
      reqmenuList: "menu/reqChangeList",
    }),
    // 封装清空
    empty() {
      this.form = {
        rolename: "", //角色名称
        menus: "",
        status: 1, //状态
      };
      this.$refs.tree.getCheckedKeys([]);
    },
    // 点击取消
    cel() {
      (this.obj.isTrue = false), this.empty();
    },
    // 点击添加
    submit() {
      // console.log(222);
      // 将数据连接到树形控件，后端要字符串
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      console.log(111);
      // 调用添加接口
      roleAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqroleList();
          this.cel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      roleInfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list,
          menus: JSON.parse(res.data.list.menus),
        };
        this.$refs.tree.setCheckedKeys(this.form.menus);
        this.form.id = id;
      });
    },
    dit() {
    this.form.menus=JSON.stringify(this.$refs.tree.getCheckedKeys());
    roleEdit(this.form).then(res=>{
      if (res.data.code==200) {
          successAlert(res.data.msg)
          this.cel()
          this.reqroleList()
      } else {
        warningAlert(res.data.msg)
      }
    })
  },
  },
  mounted() {
    this.reqmenuList();
    
  },
  // 获取一条数据

  
};
</script>
<style scoped>
</style>